var user = JSON.parse(localStorage.getItem("loggedinUser"));
if (user) {
    console.log("Currently logged in: " + user["name"]);
    document.getElementById("name-goes-here").innerText = user["name"];
} else {
    console.log("No one is logged in");
}

// 假设这里再次添加判断1
var user = JSON.parse(localStorage.getItem("loggedinUser"));
if (user) {
    // 这里可以添加当用户已登录时要执行的其他逻辑
    console.log("再次确认用户已登录，可进行其他操作");
} else {
    console.log("再次确认，还是没有用户登录");
}

// 定义 updateUsersCount 函数
function updateUsersCount(email, countValue) {
    // 从 localStorage 中获取所有用户信息
    var users = JSON.parse(localStorage.getItem("users"));
    if (users) {
        // 查找匹配邮箱的用户
        const person = users.find(u => u.email === email);
        // 如果找到匹配的用户，更新其 count 值
        if (person) {
            person.count = countValue;
            // 新增代码：添加到历史记录
            addToHistory(person); 
        }
        // 将更新后的用户数组存回 localStorage
        localStorage.setItem("users", JSON.stringify(users));
    }
}

function addCupListener() {
    document.getElementById("addCupButton").addEventListener("click", function () {
        if (user) {
            // increment the counter 
            user["count"]++;
            // save into loggedinUser 
            localStorage.setItem("loggedinUser", JSON.stringify(user));
            // display it 
            document.getElementById("coffee-count-goes-here").innerText = user["count"];

            // get the users list, and update the count for THAT user 
            updateUsersCount(user["email"], user["count"]);
        } else {
            console.log("请先登录再添加咖啡杯数。");
        }
    });
}
function addToHistory(person) {
    const now = new Date();
    const formatted = now.toLocaleString('en-US', {
        weekday: 'long',  // "Monday", "Tuesday", etc.
        year: 'numeric',  // "2021", "2022", etc.
        month: 'long',    // "January", "February", etc.
        day: 'numeric',   // 1, 2, 3, etc.
        hour: 'numeric',  // 12 AM, 1 PM, etc.
        minute: 'numeric',
        second: 'numeric'
    });
    // 判断person.history是否为null或者未定义，如果是则初始化为空数组
    if (person.history === null || person.history === undefined) {
        person.history = [];
    }
    person.history.push({ date: formatted });
}
addCupListener();
function getCount() { 
    var who = JSON.parse(localStorage.getItem("loggedinUser")); 
    if (who) { 
        document.getElementById("coffee-count-goes-here").innerText = who["count"]; 
    } else { 
        console.log("No one is logged in"); 
    }
} 
getCount();

function addPlusMinusListener() { 
    document.addEventListener("DOMContentLoaded", function () { 
        const minusBtn = document.getElementById('minus-btn'); 
        const plusBtn = document.getElementById('plus-btn'); 
        const input = document.getElementById('number-input'); 

        minusBtn.addEventListener('click', function () { 
            const currentValue = Number(input.value) || 0; 
            if (currentValue > 0) { 
                input.value = currentValue - 1; 
            } 
        }); 
        plusBtn.addEventListener('click', function () { 
            // const currentValue = Number(input.value) || 0; 
            input.value++; 
        }); 
  
    });
} 
addPlusMinusListener(); 
 
function addCupsListener() { 
    document.getElementById("addCupsButton").addEventListener("click", function () { 
        var number = document.getElementById("number-input").value; 
 
        // find out who is logged in 
        var user = JSON.parse(localStorage.getItem("loggedinUser")); 
        if (user) { 
            //increment the counter,  save into loggedinUser 
user["count"] = Number(user["count"]) + Number(number); 
localStorage.setItem("loggedinUser", JSON.stringify(user));
//get the users collection, and update the count for THAT user 
updateUsersCount(user["email"], user["count"]); 
// display it 
document.getElementById("coffee-count-goes-here").innerText = user["count"]; 
 
} else { 
console.log("No one is logged in"); 
} 
})
     
} 
addCupsListener(); 